% matches correlations Spearman or Pearson 
corrMethod='Pearson';%'Spearman';%

% --- data ----
 Rdelays=delays_Matches_Click;
 Rscores=scores_Matches_Click;
 Rsubjects=subjects_Matches_Click;


% ---  Within subjects correlations and sem ---
people_id=unique(Rsubjects); % list of subjects ID
people_num=length(people_id); % length of subjects ID
individual_corrs=zeros(1, people_num); % this is an array to store within subject Spearman R
for i=1:people_num
    x=Rscores(Rsubjects==people_id(i));
    y=Rdelays(Rsubjects==people_id(i));
    individual_corrs(1,i)=corr(x,y,'type',corrMethod).^2;
end
individual_corrs=individual_corrs(~isnan(individual_corrs));
meanIndividualCorrs=mean(individual_corrs);
individual_se=std(individual_corrs)./sqrt(people_num);
% results
% meanIndividualCorrs
% individual_se


% --- Pooled correlations with bootstrap error bars ---
% pooled Spearman Corrs
SpearmanPoolCorr=corr(Rdelays, Rscores,'type',corrMethod).^2;
% results: SpearmanPoolCorr

% Bootstrpa by resampling subjects with replacement 1000 times
PoolCorrBoot=zeros(1000,1);
for boot=1:1000
    tmpScore=[];
    tmpDelay=[];
    for i=1:people_num
        rnd=floor(rand()*people_num)+1; % find a random subject
        randSubj=people_id(rnd);
        tmpDelay=[tmpDelay; Rdelays(Rsubjects==randSubj)];
        tmpScore=[tmpScore; Rscores(Rsubjects==randSubj)];
    end
    PoolCorrBoot(boot)=corr(tmpDelay, tmpScore,'type',corrMethod).^2;
end
PoolCorrBoot=sort(PoolCorrBoot);
% results CI
% PoolCorrBoot(50)
% PoolCorrBoot(950)



% --- Averaged correlations with boot strap error bars ---
% The first 20 delaysMatchesClick are used as Key to number of matches presented
% First we average scores for each of these presentations to estimate crowed wisdom
MatchesNums=Rdelays(1:20); % number of matches in each image x 20 images
averagedScores=zeros(20,1);
for i=1:20
    averagedScores(i)=mean(Rscores(Rdelays==Rdelays(i)));
end
AvgCorr=corr(MatchesNums,averagedScores,'type',corrMethod).^2;
% results: AvgCorr

% Bootstrpa by resampling subjects with replacement 1000 times
AvgCorrBoot=zeros(1000,1);
for boot=1:1000
    tmpScore=[];
    tmpDelay=[];
    for i=1:people_num
        rnd=floor(rand()*people_num)+1; % find a random subject
        randSubj=people_id(rnd);
        tmpDelay=[tmpDelay; Rdelays(Rsubjects==randSubj)];
        tmpScore=[tmpScore; Rscores(Rsubjects==randSubj)];
    end
    
    for i=1:20
        averagedScores(i)=mean(tmpScore(tmpDelay==tmpDelay(i)));
    end
    AvgCorrBoot(boot)=corr(MatchesNums,averagedScores,'type',corrMethod).^2;
end
AvgCorrBoot=sort(AvgCorrBoot);
% results CI
% AvgCorrBoot(50)
% AvgCorrBoot(950)


% --- AUC (area under curve) with bootstrap error bars --- 
% We first compute the average (crowd wisdom correlations
% We average all estimates per image and correlate the real number of
% matches with the average estimate
% Second, we compute bootstrap error bars by resampling with replacement

% compute Area Under Curve
averagedScores=zeros(20,1);
avgCorrs=zeros(34,100);
for iter=1:100 % 100 iterations of sampling 
    for j=2:35 % from 2 and up to 35 random subjects
        for i=1:20 % for each of the 20 images
            tmpS=Rscores(Rdelays==Rdelays(i));
            rnd=rand(length(tmpS),1);
            [~,Bsort]=sort(rnd); %Get the order of B
            tmpS=tmpS(Bsort);
            averagedScores(i)=mean(tmpS(1:j));
        end
        avgCorrs(j-1,iter)=corr(MatchesNums, averagedScores,'type',corrMethod).^2;
    end
end
AUCgraph=mean(avgCorrs,2);
AUC=mean(mean(avgCorrs));
% results: AUC


% now compute error bars for AUC using resampling with replacement of subjects bootstrap
aucMatchesboot=zeros(1000,1);
for boot=1:1000
    tmpScore=[];
    tmpDelay=[];
    for i=1:people_num
        rnd=floor(rand()*people_num)+1;
        randSubj=people_id(rnd);
        tmpDelay=[tmpDelay;Rdelays(Rsubjects==randSubj)];
        tmpScore=[tmpScore;Rscores(Rsubjects==randSubj)];
    end
    
  % compute AUC 
    for iter=1:100 % 100 iterations of sampling 
        for j=2:35 % from 2 and up to 35 random subjects
            for i=1:20 % for each of the 20 images
                tmpS=tmpScore(tmpDelay==tmpDelay(i));
                rnd=rand(length(tmpS),1);
                [~,Bsort]=sort(rnd); %Get the order of B
                tmpS=tmpS(Bsort);
                averagedScores(i)=mean(tmpS(1:j));
            end
            avgCorrs(j-1,iter)=corr(MatchesNums, averagedScores,'type',corrMethod).^2;
        end
    end
    aucMatchesboot(boot)=mean(mean(avgCorrs));  
end
aucMatchesboot=sort(aucMatchesboot);
% results
% confLow=aucMatchesboot(50)
% confHigh=aucMatchesboot(950)
  
  